#include "mainwindow.h"

#include "ui_mainwindow.h"

#include <qtimeline>

#include "openglctx.h"
#include "changenotifydialog.h"
#include "stl.h"

#include <iostream>
#include <iomanip>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    setupUi(this);

    lineEdit->setValidator(new QRegExpValidator(QRegExp("[A-Z]*"), this));
    // Construct a 1-second timeline with a frame range of 0 - 100
    QTimeLine *timeLine = new QTimeLine(1000, this);
    timeLine->setFrameRange(0, 100);
    timeLine->setCurveShape(QTimeLine::LinearCurve);

    connect(timeLine, SIGNAL(frameChanged(int)), indicator, SLOT(setValue(int)));

    // Clicking the push button will start the progress bar animation
    connect(start_button, SIGNAL(clicked()), timeLine, SLOT(start()));

    QMenu *fileMenu = menuBarka->addMenu(tr("&File"));
    actionNew = fileMenu->addAction("&New");
    actionOpen = fileMenu->addAction("&Open");
    actionOpen->setShortcut(tr("Ctrl+O"));
    QMenu *editMenu = menuBarka->addMenu(tr("&Edit"));
    editMenu->addAction("&Cut");
    editMenu->addAction("&Paste");
    QMenu *toolsMenu = menuBarka->addMenu(tr("&Tools"));
    actionNotify = toolsMenu->addAction("&Notify upon FS changes");


    widgetGL = new OpenGLctx(this);
    verticalLayout->addWidget(widgetGL);

    connect(actionOpen, SIGNAL(triggered()), this, SLOT(openFile()));
    connect(actionNotify, SIGNAL(triggered()), this, SLOT(notifyDialog()));

    tcpReader = new TcpDataReader(this);

    connect(connectButton, SIGNAL(clicked()), this, SLOT(connectToServer()));
    connect(tcpReader, SIGNAL(connected(bool)), this, SLOT(on_connectedToServer(bool)));
    connect(tcpReader, SIGNAL(newData()), this, SLOT(processTcpData()));
    connect(tcpReader, SIGNAL(error()), this, SLOT(on_TcpError()));

}

MainWindow::~MainWindow()
{
}

void MainWindow::openFile()
{
    qDebug("opening file");
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "/tmp/", tr("STL Files (*.stl)"));

    QMessageBox::information(this, "File selected", fileName);
    stl_file stl_src(qPrintable(fileName));
    stl_src.load();

    widgetGL->setMesh(stl_src.triangles);
}

void MainWindow::notifyDialog()
{
    ChangeNotifyDialog changeNotifyDialog;
    changeNotifyDialog.exec();

}

void MainWindow::on_connectedToServer(bool connected)
{
    if(connected)
    {
	connectButton->setText(tr("Disconnect"));
    }
    else
    {
	connectButton->setText(tr("Connect"));
    }
    connectButton->setEnabled(true);

}


void MainWindow::connectToServer()
{
    connectButton->setEnabled(false);

    if(!tcpReader->isConnected())
	tcpReader->connectToServer();
    else
	tcpReader->disconnectFromServer();

}

void MainWindow::processTcpData()
{
    std::cout << tcpReader->getNFrames() << " frames available" << std::endl;

    QByteArray data = tcpReader->popData();

    std::cout
	    << data.size()
	    << " bytes in popped frame, "
	    << tcpReader->getNFrames()
	    << " frames available"
	    << endl;

    float *float_data = reinterpret_cast<float *>(data.data());

    int length = data.size() / sizeof(*float_data);
    int nVertices = length / 3;
    int nFaces = nVertices / 3;

    vector<triangle3d> triangles;
    triangles.reserve(nFaces);

    for(int face = 0; face < nFaces; face++)
    {
	triangle3d triangle;

	for(int vertice = 0; vertice < 3; vertice++)
	{
	    /*
    	    std::cout
		    << std::setprecision(5)
		    << float_data[i + 0] << " "
		    << float_data[i + 1] << " "
		    << float_data[i + 2] << " "
		    << std::endl;
	    */
	    for(int coord = 0; coord < 3; coord++)
		triangle.points[vertice].coords[coord] = *float_data++;

	}
//	std::cout << std::endl;

	triangles.push_back(triangle);
    }
    widgetGL->setMesh(triangles);

}
